package cn.enjoy.es.original.query;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * @author K
 * @date 2021/8/31 下午2:05
 */
public class DocQuery {
    public static void main(String[] args) throws IOException {
        // 创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

        // 查询请求体
        SearchRequest request = new SearchRequest();
        request.indices("user");
//        // 1. 查询所有
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.matchAllQuery());
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        // 查询匹配
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            //输出每条查询的结果信息
//            System.out.println(hit.getSourceAsString());
//        }
//        System.out.println("<<========");

//        // 2. 匹配查询
//        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 30)));
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 3. 分页查询
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.matchAllQuery());
//        // from (当前页-1) * 每页数量，下一页传2 再下一页传4
//        sourceBuilder.from(0);
//        sourceBuilder.size(2);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 4. 排序
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.matchAllQuery());
//
//        sourceBuilder.sort("age", SortOrder.ASC);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 5. 过滤字段
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.matchAllQuery());
//
//        String[] excludes = {};
//        String[] includes = {"name", "age"};
//        sourceBuilder.fetchSource(includes, excludes);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 6. bool 多条件查询
//        // must 分词匹配 相当于 and xx=yy
//        // should 相当于 or xx=yy
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        BoolQueryBuilder rangeQuery = QueryBuilders.boolQuery();
//        rangeQuery.must(QueryBuilders.matchQuery("age", "30"));
//        rangeQuery.must(QueryBuilders.matchQuery("name", "李四"));
//        rangeQuery.should(QueryBuilders.matchQuery("sex", "男"));
//
//        sourceBuilder.query(rangeQuery);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 7. 范围查询
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
//        rangeQuery.gte(20);
//        rangeQuery.lte(30);
//
//        sourceBuilder.query(rangeQuery);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 8. 模糊查询 Fuzziness.ONE 偏差一个字符存储中文时，查询不准确
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "王").fuzziness(Fuzziness.ONE);
//
//        sourceBuilder.query(fuzzyQueryBuilder);
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//        }

//        // 9. 高亮查询
//        // 只能高亮字符，不能高亮数字
//        // 只能高亮一个field
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//
//        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("name", "张三");
//        sourceBuilder.query(matchPhraseQueryBuilder);
//
////        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("sex", "男");
////        sourceBuilder.query(termQueryBuilder);
////        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", 20);
//
//        // 高亮
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        highlightBuilder.field("name");
//        highlightBuilder.preTags("<font color='red'>");
//        highlightBuilder.postTags("</font>");
//        sourceBuilder.highlighter(highlightBuilder);
//
//        request.source(sourceBuilder);
//
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        SearchHits hits = response.getHits();
//        System.out.println("took:" + response.getTook());
//        System.out.println("timeout:" + response.isTimedOut());
//        System.out.println("total:" + hits.getTotalHits());
//        System.out.println("MaxScore:" + hits.getMaxScore());
//        System.out.println("hits========>>");
//        for (SearchHit hit : hits) {
//            System.out.println(hit.getSourceAsString());
//            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//            System.out.println(highlightFields);
//        }

//        // 10. 聚合查询
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        MaxAggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
//        sourceBuilder.aggregation(aggregationBuilder);
//
//        request.source(sourceBuilder);
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//        System.out.println(response);

        // 11. 分组查询
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("maxGroup").field("age");
        sourceBuilder.aggregation(aggregationBuilder);

        request.source(sourceBuilder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
        System.out.println(response);

        esClient.close();

    }
}
